package io.shallwe.history;

import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.jfinal.aop.Aop;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.*;
import io.shallwe._admin.line.LineInfoService;
import io.shallwe.common.model.LineInfo;
import io.shallwe.common.model.LocationDetail;
import io.shallwe.common.model.Toc;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 参数曲线服务
 * @author : 王小伟
 * @date : 2019-07-23 15:09:08
 */
public class ParameterCurveService {

    private SiemensS7Net plc=null;
    @Inject
    LineInfoService lineInfoService = Aop.get(LineInfoService.class);

    @Inject
    LogTocService tocService;

    public ParameterCurveService(){
        plc=new SiemensS7Net(SiemensPLCS.S1500, PropKit.get("PLC","172.30.31.1"));
        plc.ConnectServer();
        plc.SetPersistentConnection();
    }

    public Ret paginate(Integer offset,String barcode,String traceCode,String rackNo,String materialNo){
    	Integer pageNumber=offset/100+1;
        Kv condition =Kv.by("barcode",barcode).set("traceCode",traceCode).set("rackNo",rackNo).set("materialNo",materialNo);
        Page<Record> page=Db.use("pg").template("history.paginate", condition).paginate(pageNumber, 100);
        return Ret.ok().set("rows",page.getList()).set("total",page.getTotalRow());
    }



    public Ret getData(String tableName, String startTime, String endTime) {
        Kv condition = Kv.by("tableName",tableName).set("startTime",startTime).set("endTime",endTime);
        List<Record> records = Db.use("pg").template("history.getData", condition).find();
        return  Ret.ok("data",records);
    }


    /**
     *
     * @param tableName
     * @param startTime
     * @param endTime
     * @return
     */
    public Record getParameterSummaryData(String tableName, String startTime, String endTime){
        Kv condition = Kv.by("tableName",tableName).set("startTime",startTime).set("endTime",endTime);
        DbPro pg = Db.use("pg");
        DbTemplate template = pg.template("history.getParameterSummaryData", condition);
        Record record =template.findFirst();
        return record;
    }

    /**
     * @Description:   判定该产品是否合格
     * @param:  [barcode]
     * @return: boolean
     * @author : gkw
     */
    public boolean checkProduct(String barcode){
        if(StrKit.isBlank(barcode)){
            return false;
        }
        ZoneId zoneId = ZoneId.systemDefault();
        Kv condition = Kv.by("barcode",barcode);
        List<Record> list = Db.use("pg").template("history.checkProdect",condition).find();
        if(list.size()==0){
            return false;
        }
        for (int i=0;i<list.size();i++){
            Record record = list.get(i);
            if(record.getInt("parameter_control_flag").intValue()==Toc.do_control){
                if(record.getInt("parameter_max_value").intValue()>record.getInt("parameter_max_value_config").intValue()
                        || record.getInt("parameter_min_value").intValue()<record.getInt("parameter_min_value_config").intValue()){
                    return false;
                }
            }
            if(record.getInt("stay_time_control_flag").intValue()==1){
                LocalDateTime startTime = record.getDate("start_time").toInstant().atZone(zoneId).toLocalDateTime();
                LocalDateTime endTime = record.getDate("end_time").toInstant().atZone(zoneId).toLocalDateTime();
                Duration duration=Duration.between(startTime,endTime);
                //这边是毫秒
                long betOne = duration.toMillis();
                int dif = (int)(betOne/1000);
                if(dif>record.getInt("max_stay_time").intValue() ||dif<record.getInt("min_stay_time").intValue()){
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * @Description: 查看该产品在产线上的状态
     * @param:  [barcode]
     * @return: java.util.List<com.jfinal.plugin.activerecord.Record>
     * @author : gkw
     */
    public List<Record> findLocationDetailByBarcode(String barcode){
        ZoneId zoneId = ZoneId.systemDefault();
        Kv condition = Kv.by("barcode",barcode);

        List<Record> list = Db.use("pg").template("history.checkProdect",condition).find();
        List<Record> newList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            int flag = 0;
            Record record = list.get(i);
            if(record.getInt("parameter_control_flag").intValue()==Toc.do_control){
                if(record.getInt("parameter_max_value").intValue()>record.getInt("parameter_max_value_config").intValue()
                        || record.getInt("parameter_min_value").intValue()<record.getInt("parameter_min_value_config").intValue()){
                    flag = 1;
                }
            }
            LocalDateTime startTime = record.getDate("start_time").toInstant().atZone(zoneId).toLocalDateTime();
            LocalDateTime endTime = record.getDate("end_time").toInstant().atZone(zoneId).toLocalDateTime();
            Duration duration=Duration.between(startTime,endTime);
            //这边是毫秒
            long betOne = duration.toMillis();
            int dif = (int)(betOne/1000);
            if(record.getInt("stay_time_control_flag").intValue()==1){
                if(dif>record.getInt("max_stay_time").intValue() ||dif<record.getInt("min_stay_time").intValue()){
                    flag = 2;
                }
            }
            record.set("errorStatus",flag);
            record.set("realStayTime",dif);
            newList.add(record);
        }
        return newList;
    }

}
